package gavdi.dk.data;

import gavdi.dk.model.Customer;
import gavdi.dk.model.Event;
import gavdi.dk.model.TimeRegistration;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DAOCustomer {

	  // Database fields
	  private SQLiteDatabase database;
	  private MySQLiteHelper dbHelper;
	  private String[] allColumns = { 
	      MySQLiteHelper.COLUMN_NAME,
	      MySQLiteHelper.COLUMN_WBS,
	      MySQLiteHelper.COLUMN_DESCRIPTION,
	      MySQLiteHelper.COLUMN_PROJECTDEF };
	  
	  public DAOCustomer(Context context) {
	    dbHelper = new MySQLiteHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public String createCustomer(Customer customer) {
	    open();
		ContentValues values = new ContentValues();
		
	    values.put(MySQLiteHelper.COLUMN_NAME, customer.getName());
	    values.put(MySQLiteHelper.COLUMN_WBS, customer.getWbs());
	    values.put(MySQLiteHelper.COLUMN_DESCRIPTION, customer.getDescription());
	    values.put(MySQLiteHelper.COLUMN_PROJECTDEF, customer.getProjectDefinition());

	    long res = database.insert(MySQLiteHelper.TABLE_CUSTOMER, null,
	        values);
	    close();
	    return Long.toString(res);
	  }

	  public boolean deleteCustomer(Customer customer) {
	    open();
	    int count = database.delete(MySQLiteHelper.TABLE_CUSTOMER, MySQLiteHelper.COLUMN_ID
	        + " = " + customer.getId(), null);
	    close();
	    if(count == 0)
	    	return false;
	    return true;
	  }

	  public Customer getCustomer(String wbs) {
		open();
		
	    Cursor cursor = database.query(MySQLiteHelper.TABLE_CUSTOMER,
	        allColumns, MySQLiteHelper.COLUMN_WBS + "="+"\""+wbs+"\"", null, null, null, null);
	    if(!cursor.moveToFirst())
	    	return null;
	    Customer customer = cursorToCustomer(cursor);
	      // Make sure to close the cursor
	    cursor.close();
	    close();  
	  
	    return customer;

	    
	  }
	  public ArrayList<Customer> getAllCustomers() {
		  open();  
		  ArrayList<Customer> customers = new ArrayList<Customer>();

		  Cursor cursor = database.query(MySQLiteHelper.TABLE_CUSTOMER,
		        allColumns, null, null, null, null, null);

		  if(!cursor.moveToFirst())
		  	return null;
		  while (!cursor.isAfterLast()) {
		    	Customer customer;
		    		 customer = cursorToCustomer(cursor);
		    		 customers.add(customer);
		    		 cursor.moveToNext();
		  }
		    // Make sure to close the cursor
		  cursor.close();
		  close();
		  return customers;
		  }
	  
	  
//	  public List<Customer> getAllCustomersByUserID(List<String> stringWBSUser) {
//		  open();  
//		  List<Customer> customers = new ArrayList<Customer>();
//		  String selection = "";
//		  for(int i = 0; i < stringWBSUser.size(); i++){
//			  selection += MySQLiteHelper.COLUMN_ID+"="+stringWBSUser.get(i);
//			  if((i+1)<stringWBSUser.size()){
//				  selection +=" OR ";
//			  }
//		  }
//		  Cursor cursor = database.query(MySQLiteHelper.TABLE_CUSTOMER,
//		        allColumns, selection , null, null, null, null);
//		  
//		  cursor.moveToFirst();
//		  while (!cursor.isAfterLast()) {
//		    		 Customer customer;
//		    		 customer = cursorToCustomer(cursor);
//		    		 customers.add(customer);
//		    		 cursor.moveToNext();
//		  }
//		    // Make sure to close the cursor
//		  cursor.close();
//		  close();
//		  return customers;
//		  }
	  
	  public static Customer cursorToCustomer(Cursor cursor) {
		  Customer customer = new Customer();
		  customer.setName(cursor.getString(0));
		  customer.setWbs(cursor.getString(1));
		  customer.setDescription(cursor.getString(2));
		  customer.setProjectDefinition(cursor.getString(3));
		  
	    return customer;
	  }
	  public boolean exists(String wbs){
		  boolean res = false;
		  open();
		  
		  Cursor cursor = 	database.query(MySQLiteHelper.TABLE_CUSTOMER,
			        		new String[]{MySQLiteHelper.COLUMN_ID}, 
			        		MySQLiteHelper.COLUMN_WBS+"="+"\""+wbs+"\"", null, null, null, null);
		  res = cursor.moveToFirst();
		  cursor.close();
		  close();
		  return res;
	  }

	public void attachCustomerToEventArray(List<Event> res) {
		for (int j = 0; j < res.size(); j++) {
			if(res.get(j).getType().equalsIgnoreCase("T")){
				TimeRegistration tr = (TimeRegistration) res.get(j);
				tr.setCustomer(getCustomer(tr.getCustomer().getWbs()));
			}

		}
		
		
	}
	
}
